package com.gaj.util;

import com.gaj.entity.message.Message;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;
import java.io.StringWriter;
import java.time.format.DateTimeFormatter;
import java.util.Map;

/**
 * XML消息构造工具类
 */
public class XmlMessageBuilder {

    private static final DateTimeFormatter DATE_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

    /**
     * 从Message实体构建XML（根据消息类型自动选择构建方法）
     */
    public static String buildFromMessage(Message message) {
        if (message == null || message.getXxlx() == null) {
            return null;
        }

        Map<String, String> params = messageToParams(message);

        switch (message.getXxlx()) {
            case 1:
                return buildDataValidationMessage(params);
            case 3:
                return buildDataSendMessage(params);
            case 4:
                return buildDataReceiveMessage(params);
            case 5:
                return buildBusinessOperationMessage(params);
            case 6:
                return buildBusinessReceiveMessage(params);
            case 7:
                return buildBusinessReturnMessage(params);
            case 20:
                return buildDataFlowMessage(params);
            case 21:
                return buildPriorityScheduleMessage(params);
            case 22:
                return buildWarningMessage(params);
            default:
                System.out.println("未知的消息类型: " + message.getXxlx());
                return null;
        }
    }

    /**
     * 将Message实体转换为参数Map
     */
    private static Map<String, String> messageToParams(Message message) {
        Map<String, String> params = new java.util.HashMap<>();

        params.put("PTTYBH", message.getPttybh());
        params.put("SJBS", message.getSjbs());
        params.put("AJBH", message.getAjbh());
        params.put("AH", message.getAh());
        params.put("AJMC", message.getAjmc());
        params.put("YWLB", message.getYwlb());
        params.put("YWLBMC", message.getYwlbmc());
        params.put("LCJDBH", message.getLcjdbh());
        params.put("JSDWBH", message.getJsdwbh());
        params.put("JSDWMC", message.getJsdwmc());
        params.put("FSDWBH", message.getFsdwbh());
        params.put("FSDWMC", message.getFsdwmc());
        params.put("XXNR", message.getXxnr());

        if (message.getFssj() != null) {
            params.put("FSSJ", message.getFssj().format(DATE_FORMATTER));
        }

        Integer xxlx = message.getXxlx();
        if (xxlx != null) {
            switch (xxlx) {
                case 1:
                    params.put("JYJG", toString(message.getJyjg()));
                    params.put("SFCF", toString(message.getSfcf()));
                    break;
                case 3:
                    params.put("CFLJ", message.getCflj());
                    params.put("WJDX", toString(message.getWjdx()));
                    params.put("YXJ", toString(message.getYxj()));
                    break;
                case 4:
                    params.put("FSJG", toString(message.getFsjg()));
                    break;
                case 20:
                    params.put("LZJD", toString(message.getLzjd()));
                    break;
                case 21:
                    params.put("YXJ", toString(message.getYxjschedule()));
                    break;
            }
        }

        return params;
    }

    /**
     * 安全转换为字符串
     */
    private static String toString(Object obj) {
        return obj == null ? "" : obj.toString();
    }

    /**
     * 1. 数据发送消息 (XXLX=3)
     */
    public static String buildDataSendMessage(Map<String, String> params) {
        Document doc = DocumentHelper.createDocument();
        Element xx = doc.addElement("XX");
        Element r = xx.addElement("R");

        addElement(r, "PTTYBH", params.get("PTTYBH"));
        addElement(r, "SJBS", params.get("SJBS"));
        addElement(r, "XXLX", "3");
        addElement(r, "AJBH", params.get("AJBH"));
        addElement(r, "AH", params.get("AH"));
        addElement(r, "AJMC", params.get("AJMC"));
        addElement(r, "YWLB", params.get("YWLB"));
        addElement(r, "YWLBMC", params.get("YWLBMC"));
        addElement(r, "LCJDBH", params.get("LCJDBH"));
        addElement(r, "JSDWBH", params.get("JSDWBH"));
        addElement(r, "JSDWMC", params.get("JSDWMC"));
        addElement(r, "FSDWBH", params.get("FSDWBH"));
        addElement(r, "FSDWMC", params.get("FSDWMC"));
        addElement(r, "XXNR", params.get("XXNR"));
        addElement(r, "CFLJ", params.get("CFLJ"));
        addElement(r, "WJDX", params.get("WJDX"));
        addElement(r, "YXJ", params.get("YXJ"));
        addElement(r, "FSSJ", params.get("FSSJ"));

        return formatXml(doc);
    }

    /**
     * 2. 数据校验消息 (XXLX=1)
     */
    public static String buildDataValidationMessage(Map<String, String> params) {
        Document doc = DocumentHelper.createDocument();
        Element xx = doc.addElement("XX");
        Element r = xx.addElement("R");

        addElement(r, "PTTYBH", params.get("PTTYBH"));
        addElement(r, "SJBS", params.get("SJBS"));
        addElement(r, "XXLX", "1");
        addElement(r, "AJBH", params.get("AJBH"));
        addElement(r, "AH", params.get("AH"));
        addElement(r, "AJMC", params.get("AJMC"));
        addElement(r, "YWLB", params.get("YWLB"));
        addElement(r, "YWLBMC", params.get("YWLBMC"));
        addElement(r, "LCJDBH", params.get("LCJDBH"));
        addElement(r, "JSDWBH", params.get("JSDWBH"));
        addElement(r, "JSDWMC", params.get("JSDWMC"));
        addElement(r, "FSDWBH", params.get("FSDWBH"));
        addElement(r, "FSDWMC", params.get("FSDWMC"));
        addElement(r, "XXNR", params.get("XXNR"));
        addElement(r, "JYJG", params.get("JYJG"));
        addElement(r, "SFCF", params.get("SFCF"));
        addElement(r, "FSSJ", params.get("FSSJ"));

        return formatXml(doc);
    }

    /**
     * 3. 数据接收消息 (XXLX=4)
     */
    public static String buildDataReceiveMessage(Map<String, String> params) {
        Document doc = DocumentHelper.createDocument();
        Element xx = doc.addElement("XX");
        Element r = xx.addElement("R");

        addElement(r, "PTTYBH", params.get("PTTYBH"));
        addElement(r, "SJBS", params.get("SJBS"));
        addElement(r, "XXLX", "4");
        addElement(r, "AJBH", params.get("AJBH"));
        addElement(r, "AH", params.get("AH"));
        addElement(r, "AJMC", params.get("AJMC"));
        addElement(r, "YWLB", params.get("YWLB"));
        addElement(r, "YWLBMC", params.get("YWLBMC"));
        addElement(r, "LCJDBH", params.get("LCJDBH"));
        addElement(r, "JSDWBH", params.get("JSDWBH"));
        addElement(r, "JSDWMC", params.get("JSDWMC"));
        addElement(r, "FSDWBH", params.get("FSDWBH"));
        addElement(r, "FSDWMC", params.get("FSDWMC"));
        addElement(r, "XXNR", params.get("XXNR"));
        addElement(r, "FSJG", params.get("FSJG"));
        addElement(r, "FSSJ", params.get("FSSJ"));

        return formatXml(doc);
    }

    /**
     * 4. 业务操作消息 (XXLX=5)
     */
    public static String buildBusinessOperationMessage(Map<String, String> params) {
        Document doc = DocumentHelper.createDocument();
        Element xx = doc.addElement("XX");
        Element r = xx.addElement("R");

        addElement(r, "PTTYBH", params.get("PTTYBH"));
        addElement(r, "SJBS", params.get("SJBS"));
        addElement(r, "XXLX", "5");
        addElement(r, "AJBH", params.get("AJBH"));
        addElement(r, "AH", params.get("AH"));
        addElement(r, "AJMC", params.get("AJMC"));
        addElement(r, "YWLB", params.get("YWLB"));
        addElement(r, "YWLBMC", params.get("YWLBMC"));
        addElement(r, "LCJDBH", params.get("LCJDBH"));
        addElement(r, "JSDWBH", params.get("JSDWBH"));
        addElement(r, "JSDWMC", params.get("JSDWMC"));
        addElement(r, "FSDWBH", params.get("FSDWBH"));
        addElement(r, "FSDWMC", params.get("FSDWMC"));
        addElement(r, "XXNR", params.get("XXNR"));
        addElement(r, "FSSJ", params.get("FSSJ"));

        return formatXml(doc);
    }

    /**
     * 5. 业务接收消息 (XXLX=6)
     */
    public static String buildBusinessReceiveMessage(Map<String, String> params) {
        Document doc = DocumentHelper.createDocument();
        Element xx = doc.addElement("XX");
        Element r = xx.addElement("R");

        addElement(r, "PTTYBH", params.get("PTTYBH"));
        addElement(r, "SJBS", params.get("SJBS"));
        addElement(r, "XXLX", "6");
        addElement(r, "AJBH", params.get("AJBH"));
        addElement(r, "AH", params.get("AH"));
        addElement(r, "AJMC", params.get("AJMC"));
        addElement(r, "YWLB", params.get("YWLB"));
        addElement(r, "YWLBMC", params.get("YWLBMC"));
        addElement(r, "LCJDBH", params.get("LCJDBH"));
        addElement(r, "JSDWBH", params.get("JSDWBH"));
        addElement(r, "JSDWMC", params.get("JSDWMC"));
        addElement(r, "FSDWBH", params.get("FSDWBH"));
        addElement(r, "FSDWMC", params.get("FSDWMC"));
        addElement(r, "XXNR", params.get("XXNR"));
        addElement(r, "FSSJ", params.get("FSSJ"));

        return formatXml(doc);
    }

    /**
     * 6. 业务退回消息 (XXLX=7)
     */
    public static String buildBusinessReturnMessage(Map<String, String> params) {
        Document doc = DocumentHelper.createDocument();
        Element xx = doc.addElement("XX");
        Element r = xx.addElement("R");

        addElement(r, "PTTYBH", params.get("PTTYBH"));
        addElement(r, "SJBS", params.get("SJBS"));
        addElement(r, "XXLX", "7");
        addElement(r, "AJBH", params.get("AJBH"));
        addElement(r, "AH", params.get("AH"));
        addElement(r, "AJMC", params.get("AJMC"));
        addElement(r, "YWLB", params.get("YWLB"));
        addElement(r, "YWLBMC", params.get("YWLBMC"));
        addElement(r, "LCJDBH", params.get("LCJDBH"));
        addElement(r, "JSDWBH", params.get("JSDWBH"));
        addElement(r, "JSDWMC", params.get("JSDWMC"));
        addElement(r, "FSDWBH", params.get("FSDWBH"));
        addElement(r, "FSDWMC", params.get("FSDWMC"));
        addElement(r, "XXNR", params.get("XXNR"));
        addElement(r, "FSSJ", params.get("FSSJ"));

        return formatXml(doc);
    }

    /**
     * 7. 数据流消息 (XXLX=20)
     */
    public static String buildDataFlowMessage(Map<String, String> params) {
        Document doc = DocumentHelper.createDocument();
        Element xx = doc.addElement("XX");
        Element r = xx.addElement("R");

        addElement(r, "PTTYBH", params.get("PTTYBH"));
        addElement(r, "SJBS", params.get("SJBS"));
        addElement(r, "XXLX", "20");
        addElement(r, "AJBH", params.get("AJBH"));
        addElement(r, "AH", params.get("AH"));
        addElement(r, "AJMC", params.get("AJMC"));
        addElement(r, "YWLB", params.get("YWLB"));
        addElement(r, "YWLBMC", params.get("YWLBMC"));
        addElement(r, "LCJDBH", params.get("LCJDBH"));
        addElement(r, "JSDWBH", params.get("JSDWBH"));
        addElement(r, "JSDWMC", params.get("JSDWMC"));
        addElement(r, "FSDWBH", params.get("FSDWBH"));
        addElement(r, "FSDWMC", params.get("FSDWMC"));
        addElement(r, "LZJD", params.get("LZJD"));
        addElement(r, "XXNR", params.get("XXNR"));
        addElement(r, "FSSJ", params.get("FSSJ"));

        return formatXml(doc);
    }

    /**
     * 8. 优先级调度消息 (XXLX=21)
     */
    public static String buildPriorityScheduleMessage(Map<String, String> params) {
        Document doc = DocumentHelper.createDocument();
        Element xx = doc.addElement("XX");
        Element r = xx.addElement("R");

        addElement(r, "PTTYBH", params.get("PTTYBH"));
        addElement(r, "SJBS", params.get("SJBS"));
        addElement(r, "XXLX", "21");
        addElement(r, "AJBH", params.get("AJBH"));
        addElement(r, "AH", params.get("AH"));
        addElement(r, "AJMC", params.get("AJMC"));
        addElement(r, "YWLB", params.get("YWLB"));
        addElement(r, "YWLBMC", params.get("YWLBMC"));
        addElement(r, "LCJDBH", params.get("LCJDBH"));
        addElement(r, "JSDWBH", params.get("JSDWBH"));
        addElement(r, "JSDWMC", params.get("JSDWMC"));
        addElement(r, "FSDWBH", params.get("FSDWBH"));
        addElement(r, "FSDWMC", params.get("FSDWMC"));
        addElement(r, "YXJ", params.get("YXJ"));
        addElement(r, "XXNR", params.get("XXNR"));
        addElement(r, "FSSJ", params.get("FSSJ"));

        return formatXml(doc);
    }

    /**
     * 9. 预警消息 (XXLX=22)
     */
    public static String buildWarningMessage(Map<String, String> params) {
        Document doc = DocumentHelper.createDocument();
        Element xx = doc.addElement("XX");
        Element r = xx.addElement("R");

        addElement(r, "PTTYBH", params.get("PTTYBH"));
        addElement(r, "SJBS", params.get("SJBS"));
        addElement(r, "XXLX", "22");
        addElement(r, "AJBH", params.get("AJBH"));
        addElement(r, "AH", params.get("AH"));
        addElement(r, "AJMC", params.get("AJMC"));
        addElement(r, "YWLB", params.get("YWLB"));
        addElement(r, "YWLBMC", params.get("YWLBMC"));
        addElement(r, "LCJDBH", params.get("LCJDBH"));
        addElement(r, "JSDWBH", params.get("JSDWBH"));
        addElement(r, "JSDWMC", params.get("JSDWMC"));
        addElement(r, "FSDWBH", params.get("FSDWBH"));
        addElement(r, "FSDWMC", params.get("FSDWMC"));
        addElement(r, "XXNR", params.get("XXNR"));
        addElement(r, "FSSJ", params.get("FSSJ"));

        return formatXml(doc);
    }

    /**
     * 添加元素的辅助方法
     */
    private static void addElement(Element parent, String name, String value) {
        parent.addElement(name).setText(value == null ? "" : value);
    }

    /**
     * 格式化XML输出
     */
    private static String formatXml(Document doc) {
        try {
            OutputFormat format = OutputFormat.createPrettyPrint();
            format.setEncoding("UTF-8");
            StringWriter sw = new StringWriter();
            XMLWriter writer = new XMLWriter(sw, format);
            writer.write(doc);
            writer.close();
            return sw.toString();
        } catch (Exception e) {
            e.printStackTrace();
            return doc.asXML();
        }
    }

    /**
     * 使用示例
     */
    public static void main(String[] args) {
        // 示例：构建数据发送消息
        Map<String, String> params = new java.util.HashMap<>();
        params.put("PTTYBH", "12345");
        params.put("SJBS", "DATA001");
        params.put("AJBH", "AJ2025001");
        params.put("AH", "AH2025001");
        params.put("AJMC", "测试案件");
        params.put("YWLB", "1");
        params.put("YWLBMC", "提请批准逮捕");
        params.put("LCJDBH", "LC001");
        params.put("JSDWBH", "JS001");
        params.put("JSDWMC", "接收单位");
        params.put("FSDWBH", "FS001");
        params.put("FSDWMC", "发送单位");
        params.put("XXNR", "数据已发送到中心平台的前置机，请接收数据！");
        params.put("CFLJ", "test.zip");
        params.put("WJDX", "1024");
        params.put("YXJ", "1");
        params.put("FSSJ", "2025-09-29 10:30:00");

        String xml = buildDataSendMessage(params);
        System.out.println(xml);
    }
}
